package zhai.docking.alipay.service.base.alipay.api;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.FileItem;
import com.alipay.api.request.AlipayOpenSpImageUploadRequest;
import com.alipay.api.response.AlipayOpenSpImageUploadResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhai.docking.alipay.constant.AlipayConstant;
import zhai.docking.alipay.constant.LogConstant;
import zhai.docking.alipay.domain.base.alipay.BaseAlipayImageUploadIn;
import zhai.docking.alipay.domain.base.alipay.BaseAlipayImageUploadOut;
import zhai.docking.alipay.domain.base.alipay.common.BaseAlipayCommonOut;
import zhai.docking.alipay.domain.base.bizlog.BizLogDataTemp;
import zhai.docking.alipay.domain.base.bizlog.BizLogSave;
import zhai.docking.alipay.prop.AlipayApiProperties;
import zhai.docking.alipay.service.base.bizlog.BizLogSer;
import zhai.docking.alipay.util.JSONUtil;
import zhai.docking.alipay.util.ObjectUtil;
import zhai.docking.alipay.util.StringUtil;

/**
 * @author Created by 翟凤玺
 * @package zhai.docking.alipay.service.base.alipay.api
 * @project-name zhai-docking-alipay
 * @description 基础-支付宝-图片 服务实现
 * @create-datetime 2020/12/30 11:52
 */
@Slf4j
@Service
public class BaseAlipayApiImageSerImpl implements BaseAlipayApiImageSer {

    private static final String BUSINESS_NAME = "【基础-支付宝-新蓝海 服务】";
    private static final String METHOD_UPLOAD = "（alipay.open.sp.image.upload(图片上传接口) https://opendocs.alipay.com/apis/01ea4t）";

    @Autowired
    AlipayApiProperties alipayApiProp;
    @Autowired
    BizLogSer bizLogSer;

    @SneakyThrows
    @Override
    public BaseAlipayImageUploadOut upload(BaseAlipayImageUploadIn in) {
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_UPLOAD, LogConstant.IN, JSONUtil.toStr(in)));
        /** 操作：调用支付宝接口 */
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConstant.COMMON_SERVER_URL, alipayApiProp.getAppid(), alipayApiProp.getPrivateKey(), AlipayConstant.COMMON_FORMAT, AlipayConstant.COMMON_CHARSET, alipayApiProp.getPublicKey(), AlipayConstant.COMMON_SIGN_TYPE);
        AlipayOpenSpImageUploadRequest request = new AlipayOpenSpImageUploadRequest();
        request.setImageContent(new FileItem(in.getImageFile()));
        AlipayOpenSpImageUploadResponse response = alipayClient.execute(request);
        /** 操作：记录业务日志 */
        bizLogSer.save(new BizLogSave(new BizLogDataTemp(in, response), AlipayConstant.IMAGE_UPLOAD_LOG));
        /** 操作：结果转换 */
        BaseAlipayImageUploadOut result = ObjectUtil.copy(response, BaseAlipayImageUploadOut.class);
        BaseAlipayCommonOut commonOut = result.commonOut(response);
        ObjectUtil.copy(commonOut, result);
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_UPLOAD, LogConstant.RESULT, LogConstant.SUCCESS, JSONUtil.toStr(result)));
        return result;
    }
}